/**
 * @author lonun
 * @blog http://www.lonun.com
 */

var models = require('../model/models');

exports.index = function(form) {
	return {};
}

exports.newUser = function(form) {
	return {};
}

exports.saveUser = function(form) {
	var User = models.get('User');
	var user = new User();
	this.fill(user);
	user.save(function(err) {
		if (err) {
			console.log(err);
		}
		console.log('save user');
	});
	return {
		msg : 'ok'
	};
}

exports.login = function(form) {
	var User = models.get('User');
	var userName = form.getParam('username');
	var passwd = form.getParam('passwd');
	if (userName && passwd) {
		var user = User.findOne({
			username : userName
		});
		if (user.passwd == passwd) {
			var session = form.getSession();
			session.attr('currentUser', user);
			return {
				msg : 'ok'
			};
		} else {
			return {
				msg : 'fails'
			};
		}
	} else {
		return {
			'_r' : '/user/login'
		};
	}
}
